% MRQFIT - nonlinear curve fitter
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%    [FP, CHISQ, NITER] = MRQFIT('exponential', P, X, Y) fits a sum
%    of N exponential functions with time constants Ti and amplitudes
%    Ai to the data specified by X and Y:
%
%       F(X) = A0 + A1*exp(-t/T1) + A2*exp(-t/T2) + ... + AN*exp(-t/TN)
%
%    The parameter vector P must have 2N + 1 parameters for N exponentials:
%
%                    P = [A0 A1 T1 A2 T2 ... AN TN]
%
%    The parameters found by the minimization algorithm are returned to FP,
%    the fit chi^2 normalized by the D.O.F. is returned to CHISQ, and the
%    number of iterations is returned to NITER.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%    [...] = MRQFIT('exppower', P, X, Y) fits an exponential function
%    raised to the Nth power to the data specified by X and Y:
%
%              F(X) = Imax*[A1 + (A0 - A1)*exp(-t/T)]^N
%
%    where 0 <= A0 & A1 <= 1.  The parameter vector P must have 5
%    parameters:
%
%                       P = [Imax A0 A1 T N]
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%    [...] = MRQFIT('gaussian', P, X, Y) fits a sum of N scaled Gaussian
%    functions to the data specified by X and Y:
%
%           F(X) = A0 + A1*G1(X) + A2*G2(X) + ... + AN*GN(X)
%
%    where each Gaussian Gi(X) has a mean Mi and a standard deviation Si:
%
%        Gi(X) = exp(-(X - Mi)^2 / (2*Si^2)) / (SQRT(2*PI) * Si)
%
%    The parameter vector P must have 3N + 1 parameters for N Gaussians:
%
%               P = [A0 A1 M1 S1 A2 M2 S2 ... AN MN SN]
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%    [...] = MRQFIT('boltzmann', P, X, Y) fits a Boltzmann function
%    to the data specified by X and Y.  This is the multistate form
%    of the Boltzmann function to calculate the steady-state occupancy
%    Y of the open state O in the model:
%
%                   C1 <-> C2 <-> ... <-> CN <-> O
%
%    where the transition rates are exponential functions of X.  This
%    version of the multistate Boltzmann function has the form:
%
%      F(X) = A0 + (A1 - A0)/(1 + EN + EN*EN-1 + EN*EN-1*EN-2 + ...)
%
%    where each Boltzmann Ei(X) has a slope Ki and a half-activation Xi:
%
%                      Ei(X) = exp(-Ki*(X - Xi))
%
%    The parameter vector P must have 2N + 2 elements for N states:
%
%                  P = [A0 A1 K1 X1 K2 X2 ... KN XN]
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%    [...] = MRQFIT('boltzsum', P, X, Y) fits a sum of N Boltzmanns to
%    the data specified by X and Y.  This is a linear sum of individual
%    Boltzmann functions to calculate the steady state open probability
%    when N conductances are contributing to the overall current:
%
%        F(X) = A0 + A1/(1 + E1) + A2/(1 + E2) + ... + AN/(1 + EN)
%
%    where each Boltzmann Ei(X) has a slope Ki and a half-activation Xi:
%
%                      Ei(X) = exp(-Ki*(X - Xi))
%
%    The parameter vector P must have 3N + 1 elements for N individual
%    Boltzmanns:
%
%               P = [A0 A1 K1 X1 A2 K2 X2 ... AN KN XN]
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%    [...] = MRQFIT('ivboltzsum', P, X, Y) is the current form of the
%    sum of Boltzmann functions, which includes an offset A0 and a
%    reversal potential Vr:
%
%    F(X) = A0 + (X - Vr)*[A1/(1 + E1) + A2/(1 + E2) + ... + AN/(1 + EN)]
%    
%    where each Boltzmann Ei(X) has a slope Ki and a half-activation Xi:
%
%                      Ei(X) = exp(-Ki*(X - Xi))
%
%    The parameter vector P must have 3N + 2 elements for N individual
%    Boltzmanns:
%
%              P = [A0 Vr A1 K1 X1 A2 K2 X2 ... AN KN XN]
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%    [...] = MRQFIT(..., SIG, VP, LB, UB, IMAX, TOL) provides
%    additional parameters to control the minimization algorithm.
%    Pass an empty array [] for any of these values to use the
%    default values.
%
%    SIG provides standard deviations for each data value in Y.
%    The default is 1.
%
%    VP indicates which parameters vary.  The default is all vary.
%
%    LB and UB provide lower and upper bounds on the fit parameters
%    (the default is none).  Use NaN or Inf to specify parameters
%    without lower or upper bounds.  The default is no bounds.
%
%    IMAX is a scalar containing the maximum number of iterations.
%    The default is 25.
%
%    TOL is a scalar containing the chi^2 convergence tolerance.
%    The default is 1.0e-3.
% 
%    [FP, CHISQ, NITER, FITC, ERR, DEP] = MRQFIT(...) returns the
%    resulting fit FCN(P, X) evaluated at each X (FITC), the number
%    of successful iterations (NITER), the estimated error on each
%    fit parameter (ERR), and the fit parameter dependencies (DEP).

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: February 29, 2000

% MEX file.
